package org.dragonnova.business.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dragonnova.business.model.WxConfig;
import org.dragonnova.business.service.WxService;
import org.dragonnova.business.util.WeixinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.pub.web.BaseController;

@Controller
@RequestMapping("/wx")
public class WxController extends BaseController {
	@Autowired
	private WxService wxService;

	@RequestMapping("/getWxConfig")
	@ResponseBody
	public Map<String, Object> getWxConfig(String appId,HttpServletResponse resp, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		try {
			List<WxConfig> list = wxService.findWxConfig(appId);
			WxConfig wxc = list.get(0);
			int time = (int) (System.currentTimeMillis() / 1000) - wxc.getTimestamp();
			if (time > 2 * 60 * 60) {
				wxc = WeixinUtil.getWxConfig(request, wxc);
				wxService.updateWxConfig(wxc);
			}
			map.put("date", wxc);
			map.put("result", 0);
			map.put("message", "查询成功！");
		} catch (Exception e) {
			map.put("result", -1);
			map.put("message", "数据异常！");
			e.printStackTrace();
		}
		return map;
	}

}
